package java_0308;

import java.util.Arrays;

/**
 * 字符串逆置
 * 如 "I am a student" 逆置为 "student a am I"
 */
public class ReverseString {
    public static void reverseSentence(String str){
        String[] strs=str.split(" ");//按“ ”拆分为字符串数组
        //逆置字符串数组
        int left=0;
        int right= strs.length-1;
        strs=reverse(strs,left,right);
        StringBuilder sb= new StringBuilder();
        for(String temp:strs){
            sb.append(temp+" ");
        }
        System.out.println(sb);
        return;
    }

    private static String[] reverse(String[] strs, int left, int right) {//字符串数组逆置
        String cur=strs[left];
        strs[left]=strs[right];
        strs[right]=cur;
        return strs;
    }

    public static void main(String[] args) {
        String str="I am a student";
       reverseSentence(str);
    }
}
